/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command.preview
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.text.TextField;
	import flash.text.TextFormatAlign;
	import flash.utils.Dictionary;
	
	import multimedia.control.event.preview.LoadTextEvent;
	import multimedia.model.ModelLocator;
	import multimedia.model.vo.TextVO;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.osmf.elements.ImageElement;
	import org.osmf.elements.ParallelElement;
	import org.osmf.elements.TextElement;
	import org.osmf.elements.VideoElement;
	import org.osmf.events.LoadEvent;
	import org.osmf.events.MediaElementEvent;
	import org.osmf.events.MediaErrorEvent;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.layout.LayoutMode;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaType;
	import org.osmf.media.URLResource;
	import org.osmf.traits.AudioTrait;
	import org.osmf.traits.LoadState;
	import org.osmf.traits.LoadTrait;
	import org.osmf.traits.MediaTraitType;

	/**
	 * command for loading text objects
	 */
	public class LoadTextCommand extends SequenceCommand implements ICommand
	{

		private var e:LoadTextEvent;

		private var media:ParallelElement;

		private var text:MediaElement;

		private var image:MediaElement;
		
		private var background_image:MediaElement;

		private var image_source:TextElement;
		
		private var video:MediaElement;
		
		private var imageView:ParallelElement;
		private var videoView:ParallelElement;

		private var model:ModelLocator;

		private var videoLayout:LayoutMetadata;
		private var imageLayout:LayoutMetadata;
		private var backgroundImageLayout:LayoutMetadata
		private var sourceLayout:LayoutMetadata;
		private var textLayout:LayoutMetadata;
		private var parallelLayout:LayoutMetadata;
		
		private var loadable:LoadTrait;
		
		private var sizes:Dictionary = new Dictionary();
		
		private var height:Number;
		private var width:Number;
		
		private var publish_date:TextElement;
		private var publish_date_layout:LayoutMetadata;

		override public function execute(event:CairngormEvent):void
		{
			this.e = event as LoadTextEvent;
			this.nextEvent = e.nextEvent;

			this.model = ModelLocator.getInstance();

			model.previewBackgroundColor = e.text.background_color;

			height = FlexGlobals.topLevelApplication.stage.height * 0.9;
			width = height * (16 / 9);
			
			if (width > FlexGlobals.topLevelApplication.stage.width)
			{
				width = FlexGlobals.topLevelApplication.stage.width * 0.9;
				height = width / (16 / 9);
			}
			
			initSizes();

			model.wrapper.height = height;
			model.wrapper.width = width;
			model.wrapper.x = FlexGlobals.topLevelApplication.stage.width * 0.5 - width * 0.5;
			model.wrapper.y = FlexGlobals.topLevelApplication.stage.height * 0.5 - height * 0.5;
			
			if(e.text.publish_date != null)
			{
				var format:DateFormatter = new DateFormatter();
				format.formatString = "DD.MM.YYY";
				publish_date = new TextElement(format.format(e.text.publish_date), true, TextFormatAlign.RIGHT, 50, 80);
				publish_date_layout = new LayoutMetadata();
				publish_date_layout.bottom = 10;
				publish_date_layout.right = 10;
				publish_date_layout.index = 44;
				publish_date.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, publish_date_layout);
			}
			
			var title:String = '<TEXTFORMAT><FONT COLOR="#' + e.text.title_color.toString(16) + '">' +
								"<p><b>" + e.text.title + "</b></p>\n" +
							   '</FONT></TEXTFORMAT>'
			title = e.text.hide_title ? "" : title;

			//just text
			if (e.text.image == null && e.text.video == null)
			{
				text = new TextElement( title + e.text.formated_text,  
										true, TextFormatAlign.LEFT, height, width);
				textLayout = new LayoutMetadata();
				text.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, textLayout);
				textLayout.width = width;
				textLayout.height = height;
				textLayout.x = 0;
				textLayout.y = 0;
				textLayout.index = 10;
				if(e.text.background_image != null)
				{
					background_image = new ImageElement(new URLResource(e.text.background_image));
					var parallel:ParallelElement = new ParallelElement();
					
					parallel.addChild(text);
					parallel.addChild(background_image);
					if(e.text.publish_date != null)
					{
						parallel.addChild(publish_date);
					}
					
					backgroundImageLayout = new LayoutMetadata();
					background_image.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, backgroundImageLayout);
					backgroundImageLayout.width = width;
					backgroundImageLayout.height = height;
					
					model.wrapper.addMediaElement(parallel);
					model.wrapper.media = parallel;
				} else
				{
					
					model.wrapper.addMediaElement(text);
					model.wrapper.media = text;
				}
			}
			else if(e.text.image != null)
			{
				media = new ParallelElement();
				text = new TextElement(title + e.text.formated_text,  
									   true, 
									   TextFormatAlign.LEFT, 
									   height * sizes["text-height-factor-" + e.text.image_orientation], 
									   width * sizes["text-width-factor-" + e.text.image_orientation]);

				var imageResource:URLResource = new URLResource(e.text.image.url);
				imageResource.mediaType = MediaType.IMAGE;
				image = new ImageElement(imageResource);
				image_source = new TextElement(e.text.image.image_source, false);
				
				//create a parallelView for the image side of the TextView, so that 
				//the image_source is always shown directly below the image
				imageView = new ParallelElement();
				
				//set some layout values
				imageLayout = new LayoutMetadata();
				image.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, imageLayout);
				imageLayout.index = 10;
				imageLayout.width = sizes["image-width-" + e.text.image_orientation];
				imageLayout.height = imageLayout.width / e.text.image.aspect_ratio;
				
				var imageSourceLayout:LayoutMetadata = new LayoutMetadata();
				image_source.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, imageSourceLayout);
				imageSourceLayout.index = 11;
				imageSourceLayout.x = imageLayout.height + 50;
				
				imageView.addChild(image);
				imageView.addChild(image_source);
	
				var imageViewLayout:LayoutMetadata = new LayoutMetadata();
				imageView.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, imageViewLayout);
				imageViewLayout.percentWidth = sizes["imageView-percentWidth-" + e.text.image_orientation];
				imageViewLayout.percentHeight = sizes["imageView-percentHeight-" + e.text.image_orientation];
				imageViewLayout.percentX = sizes["imageView-percentX-" + e.text.image_orientation];
				imageViewLayout.percentY = sizes["imageView-percentY-" + e.text.image_orientation];
				imageViewLayout.paddingLeft = 5;
				imageViewLayout.paddingTop = 5;
				imageViewLayout.paddingRight = 5;
				imageViewLayout.paddingBottom = 5;
				imageViewLayout.index = 10;
				imageViewLayout.layoutMode = LayoutMode.VERTICAL;//
				sizes["imageView-layoutMode-" + e.text.image_orientation];
				media.addChild(imageView);
	
				textLayout = new LayoutMetadata();
				text.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, textLayout);
				textLayout.percentWidth = sizes["text-percentWidth-" + e.text.image_orientation];
				textLayout.percentHeight = sizes["text-percentHeight-" + e.text.image_orientation];
				textLayout.percentX = sizes["text-percentX-" + e.text.image_orientation];
				textLayout.percentY = sizes["text-percentY-" + e.text.image_orientation];
				textLayout.paddingLeft = 5;
				textLayout.paddingTop = 5;
				textLayout.paddingRight = 5;
				textLayout.paddingBottom = 5;
				textLayout.index = 10;
				media.addChild(text);
				
				if(e.text.background_image != null)
				{
					background_image = new ImageElement(new URLResource(e.text.background_image));
					backgroundImageLayout = new LayoutMetadata();
					background_image.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, backgroundImageLayout);
					backgroundImageLayout.width = width;
					backgroundImageLayout.height = height;
					media.addChild(background_image);
				}
	
				parallelLayout = new LayoutMetadata();
				media.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, parallelLayout);
				parallelLayout.height = height;
				parallelLayout.width = width;
				parallelLayout.layoutMode = LayoutMode.NONE//sizes["parallelView-layoutMode-" + e.text.image_orientation];
				
				
				model.wrapper.mediaPlayer.autoPlay = false;

				model.wrapper.media = media;
				model.wrapper.addMediaElement(media);

			} else if(e.text.video != null)
			{
				media = new ParallelElement();
				text = new TextElement(title + e.text.formated_text,  
					true, 
					TextFormatAlign.LEFT, 
					height * sizes["text-height-factor-" + e.text.image_orientation], 
					width * sizes["text-width-factor-" + e.text.image_orientation]);
				
				var videoResource:URLResource = new URLResource(e.text.video.url);
				video = new VideoElement(videoResource);
				image_source = new TextElement("Quelle: ", false);
				
				//create a parallelView for the image side of the TextView, so that 
				//the image_source is always shown directly below the image
				videoView = new ParallelElement();
				
				//set some layout values
				videoLayout = new LayoutMetadata();
				videoLayout.index = 0;
				video.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, videoLayout);
				videoLayout.width = sizes["image-width-" + e.text.image_orientation];
				videoLayout.height = videoLayout.width / (e.text.video.aspect_ratio == "16:9" ? (16/9) : (4/3));
				videoLayout.paddingLeft = 5;
				videoLayout.paddingTop = 5;
				videoLayout.paddingRight = 5;
				videoLayout.paddingBottom = 5;
				
				var videoSourceLayout:LayoutMetadata = new LayoutMetadata();
				videoSourceLayout.index =1;
				image_source.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, videoSourceLayout);
				//imageSourceLayout.x = imageLayout.height + 50;
				
				videoView.addChild(video);
				videoView.addChild(image_source);
				
				var videoViewLayout:LayoutMetadata = new LayoutMetadata();
				videoView.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, videoViewLayout);
				videoViewLayout.percentWidth = sizes["imageView-percentWidth-" + e.text.image_orientation];
				videoViewLayout.percentHeight = sizes["imageView-percentHeight-" + e.text.image_orientation];
				videoViewLayout.percentX = sizes["imageView-percentX-" + e.text.image_orientation];
				videoViewLayout.percentY = sizes["imageView-percentY-" + e.text.image_orientation];
				videoViewLayout.paddingLeft = 5;
				videoViewLayout.paddingTop = 5;
				videoViewLayout.paddingRight = 5;
				videoViewLayout.paddingBottom = 5;
				videoViewLayout.index = 0;
				videoViewLayout.layoutMode = sizes["imageView-layoutMode-" + e.text.image_orientation];
				media.addChild(video);
				
				
				textLayout = new LayoutMetadata();
				text.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, textLayout);
				textLayout.percentWidth = sizes["text-percentWidth-" + e.text.image_orientation];
				textLayout.percentHeight = sizes["text-percentHeight-" + e.text.image_orientation];
				textLayout.percentX = sizes["text-percentX-" + e.text.image_orientation] - 1;
				textLayout.percentY = sizes["text-percentY-" + e.text.image_orientation];
				textLayout.paddingLeft = 5;
				textLayout.paddingTop = 5;
				textLayout.paddingRight = 5;
				textLayout.paddingBottom = 5;
				textLayout.index = 1;
				media.addChild(text);
				
				parallelLayout = new LayoutMetadata();
				media.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, parallelLayout);
				parallelLayout.height = height;
				parallelLayout.width = width;
				parallelLayout.layoutMode = LayoutMode.HORIZONTAL;
				//sizes["parallelView-layoutMode-" + e.text.image_orientation];
					
			
				video.addEventListener(MediaElementEvent.TRAIT_ADD,
					function test(event:MediaElementEvent):void
					{
					});
				/*loadable = video.getTrait(MediaTraitType.LOAD) as LoadTrait;
				loadable.addEventListener(LoadEvent.LOAD_STATE_CHANGE, onLoadableStateChange);
				loadable.load();*/
				
				model.wrapper.mediaPlayer.autoPlay = true;
				
				model.wrapper.media = media;
				model.wrapper.addMediaElement(media);
			}

			executeNextCommand();
		}
		
		private function onLoadableStateChange(event:LoadEvent):void
		{
			if(event.loadState == LoadState.READY)
			{
				trace("Video loaded");
				loadable.removeEventListener(LoadEvent.LOAD_STATE_CHANGE, onLoadableStateChange);
				
				var audible:AudioTrait = video.getTrait(MediaTraitType.AUDIO) as AudioTrait;
				audible.volume = 0;
				
			}
			
		}
		
		private function initSizes():void
		{
			sizes["image-width-left"] = width * 0.3;
			sizes["image-width-right"] = width * 0.3;
			sizes["image-width-top"] = width;
			sizes["image-width-bottom"] = width;
			
			sizes["imageView-percentWidth-left"] = 30;
			sizes["imageView-percentWidth-right"] = 30;
			sizes["imageView-percentWidth-top"] = 100;
			sizes["imageView-percentWidth-bottom"] = 100;
			
			sizes["imageView-percentHeight-left"] = 100;
			sizes["imageView-percentHeight-right"] = 100;
			sizes["imageView-percentHeight-top"] = 30;
			sizes["imageView-percentHeight-bottom"] = 30;
			
			sizes["imageView-percentX-left"] = 0;
			sizes["imageView-percentX-right"] = 70;
			sizes["imageView-percentX-top"] = 0;
			sizes["imageView-percentX-bottom"] = 0;
			
			sizes["imageView-percentY-left"] = 0;
			sizes["imageView-percentY-right"] = 0;
			sizes["imageView-percentY-top"] = 0;
			sizes["imageView-percentY-bottom"] = 70;
			
			sizes["imageView-layoutMode-left"] = LayoutMode.VERTICAL;
			sizes["imageView-layoutMode-right"] = LayoutMode.VERTICAL;
			sizes["imageView-layoutMode-top"] = LayoutMode.HORIZONTAL;
			sizes["imageView-layoutMode-bottom"] = LayoutMode.HORIZONTAL;
			
			sizes["text-percentWidth-left"] = 70;
			sizes["text-percentWidth-right"] = 70;
			sizes["text-percentWidth-top"] = 100;
			sizes["text-percentWidth-bottom"] = 100;
			
			sizes["text-percentHeight-left"] = 100;
			sizes["text-percentHeigh-right"] = 100;
			sizes["text-percentHeigh-top"] = 70;
			sizes["text-percentHeigh-bottom"] = 70;
			
			sizes["text-percentX-left"] = 32;
			sizes["text-percentX-right"] = 0;
			sizes["text-percentX-top"] = 0;
			sizes["text-percentX-bottom"] = 0;
			
			sizes["text-percentY-left"] = 0;
			sizes["text-percentY-right"] = 0;
			sizes["text-percentY-top"] = 30;
			sizes["text-percentY-bottom"] = 0;
			
			sizes["text-width-factor-left"] = 0.60;
			sizes["text-width-factor-right"] = 0.60;
			sizes["text-width-factor-top"] = 1;
			sizes["text-width-factor-bottom"] = 1;
			
			sizes["text-height-factor-left"] = 1;
			sizes["text-height-factor-right"] = 1;
			sizes["text-height-factor-top"] = 0.68;
			sizes["text-height-factor-bottom"] = 0.68;
			
			sizes["parallelView-layoutMode-left"] = LayoutMode.HORIZONTAL;
			sizes["parallelView-layoutMode-right"] = LayoutMode.HORIZONTAL;
			sizes["parallelView-layoutMode-top"] = LayoutMode.VERTICAL;
			sizes["parallelView-layoutMode-bottom"] = LayoutMode.VERTICAL;
			
		}


	}
}